// pages/profile/profile.js
const util = require('../../utils/util.js');

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUseGetUserProfile: false,
    height: 170, // 默认身高
    weight: 60,  // 默认体重
    bmi: 0,
    bmiStatus: { status: '', color: '' },
    weightRecords: [],
    totalDays: 0,
    showEditHeightModal: false,
    newHeight: ''
  },

  onLoad: function () {
    // 检查是否可以使用 getUserProfile API
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      });
    }
    
    // 加载用户信息
    this.loadUserInfo();
    
    // 加载体重记录
    this.loadWeightRecords();
  },

  onShow: function () {
    // 每次页面显示时重新加载数据
    this.loadWeightRecords();
  },

  // 加载用户信息
  loadUserInfo: function () {
    const userInfo = wx.getStorageSync('userInfo');
    
    if (userInfo) {
      this.setData({
        userInfo,
        hasUserInfo: true
      });
    }
  },

  // 加载体重记录
  loadWeightRecords: function () {
    const weightRecords = wx.getStorageSync('weightRecords') || [];
    const height = wx.getStorageSync('userHeight') || this.data.height;
    
    // 获取最新记录的体重
    const latestRecord = weightRecords.length > 0 
      ? weightRecords[weightRecords.length - 1] 
      : { weight: this.data.weight };
    
    const bmi = util.calculateBMI(latestRecord.weight, height);
    
    this.setData({
      weightRecords,
      totalDays: weightRecords.length,
      height,
      weight: latestRecord.weight,
      bmi,
      bmiStatus: util.getBMIStatus(bmi)
    });
  },

  // 获取用户信息
  getUserProfile: function () {
    wx.getUserProfile({
      desc: '用于完善用户资料',
      success: (res) => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        });
        
        // 存储用户信息
        wx.setStorageSync('userInfo', res.userInfo);
      }
    });
  },

  // 显示修改身高弹窗
  showEditHeightModal: function () {
    this.setData({
      showEditHeightModal: true,
      newHeight: this.data.height.toString()
    });
  },

  // 隐藏修改身高弹窗
  hideEditHeightModal: function () {
    this.setData({
      showEditHeightModal: false
    });
  },

  // 监听输入身高变化
  onHeightInput: function (e) {
    this.setData({
      newHeight: e.detail.value
    });
  },

  // 保存身高
  saveHeight: function () {
    const newHeight = parseInt(this.data.newHeight);
    
    if (isNaN(newHeight) || newHeight <= 0) {
      wx.showToast({
        title: '请输入有效的身高数值',
        icon: 'none'
      });
      return;
    }
    
    // 保存到本地存储
    wx.setStorageSync('userHeight', newHeight);
    
    // 隐藏弹窗并刷新数据
    this.setData({
      showEditHeightModal: false,
      height: newHeight
    });
    
    this.loadWeightRecords();
    
    wx.showToast({
      title: '身高已更新',
      icon: 'success'
    });
  },

  // 重置所有数据
  resetAllData: function () {
    wx.showModal({
      title: '重置数据',
      content: '确定要清除所有体重记录和健康计划数据吗？',
      success: res => {
        if (res.confirm) {
          // 清除本地存储的数据
          wx.removeStorageSync('weightRecords');
          wx.removeStorageSync('healthPlans');
          
          // 重新加载数据
          this.loadWeightRecords();
          
          wx.showToast({
            title: '数据已重置',
            icon: 'success'
          });
        }
      }
    });
  },

  // 关于应用
  aboutApp: function () {
    wx.showModal({
      title: '关于BodyGuard体重管理',
      content: 'BodyGuard体重管理是一款帮助用户记录体重变化和健康计划的小程序，通过每日打卡和记录，帮助用户建立健康的生活方式。\n\n版本: 1.0.0',
      showCancel: false
    });
  }
});